package Day013_线程安全.demo03_线程状态;

/**
 * @author zhangyan
 * @date 2020/5/4 11:49
 */
public class Test2_生产和消费 {
    static Object obj = new Object();

    public static void main(String[] args) {
        //
        BaoZi bz = new BaoZi();
        //
        BaoZiPu bzp = new BaoZiPu();


        //包子线程
        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();

        //吃货线程
        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();
    }
}

class BaoZi {
    public boolean flag = false;

}

class BaoZiPu extends Thread{
    BaoZi bz;

    @Override
    public void run() {
        //生产包子
        //加锁
        synchronized (bz){
            //判断是否有包子,有包子就进入无限等待
            if (bz.flag == true){
                try {
                    bz.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("包子铺开始成产包子");
            //改变包子状态
            bz.flag = true;
            System.out.println("有包子了,唤醒吃货线程");
            bz.notify();
        }
    }
}
class ChiHuo extends Thread {
    BaoZi bz;

    @Override
    public void run() {
       //吃包子
        //加锁
        synchronized (bz){
            if (bz.flag == false){
                try {
                    bz.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("吃完包子...");
            //包子吃完,唤醒包子铺生产
            bz.flag = false;
            bz.notify();
        }
    }
}